// pages/shopping/shopping.js
import {
  request
} from './api';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    warn: '加载中……',
    curTab: 0,
    current: 0,
    goodlist: [],
    goodtypeid: [],
    count: 1,
    allgood: [], //全部列表
    All: [],
    Allshow: [], //点击全部显示列表
    list: [],
    view: '',
    datalist: [],
    dataList: [],
    times: 0,
    nowint: 0,
    loading: '',
    countnum: 0,
    pagesize: 4,
    currPage: 1, //其他列表分页
    allproducts: [],
    page: 1,
    selected:1,
    timer1:'',
    timer2:'',
    timer3:'',
    timer4:'',
    loading:true,
    scrolled:false
  },
  //切换分类
  selectTab(e) {
    let index = e.target.dataset.index;
    let that = this
    that.setData({
      curTab: index,
      current: index,
      warn:''
    })
    //点击全部分类显示商品列表
    if (that.data.goodtypeid[index] == 1) {
      that.setData({
        view: "全部",
        scrolled:false
      })
    }
    //点击分类显示并请求商品列表
    else {
      that.setData({
        scrolled:false
      })
      that.showgoodlist(index);
    }
  },

  //点击分类显示并请求商品列表
  showgoodlist(index) {
    let that = this
    this.setData({
      currPage: 1
    })
    request({
      url: '/goodList',
      data: {
        good_type: that.data.goodtypeid[index],
        page: that.data.page,
        size: 32
      }
    }).then((res) => {
      if (res.data.code == 2000) {
        that.setData({
          countnum: res.data.data.count,
          list: res.data.data.data,

        })
    
        if (that.data.list.length > 0) {
          if (index != that.data.nowint) {
            that.data.datalist = []
          }
          // that.data.datalist=that.data.list
          if(that.data.datalist.length==0){
            that.data.list.forEach(item => {
              that.data.datalist.push({
                imgUrl: item.img,
                title: item.good_name,
                price: item.price,
                good_id: item.good_id
              })
            })
          }
          
         
          that.setData({
            view: "其他",
            nowint: index,
          })
            that.showdataList(index)
           
        } 
        else {
  
          clearTimeout(this.data.timer1)
          clearTimeout(this.data.timer2)
          clearTimeout(this.data.timer3)
          clearTimeout(this.data.timer4)
          that.setData({
            view: "其他",
            dataList: [],
            nowint: index,
            warn:'暂无商品',
           
          })
  
        }
      }
    })
  },
  swiperChange(e) {
    let index = e.detail.current;
    this.setData({
      curTab: index,
      current: index
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    //配置授权状态
    that.setData({
      setting_status: wx.getStorageSync('setting_status')
    })
    // 商品展示接口
    request({
        url: '/goodType'
      })
      .then((el) => {
        var goodlist = [];
        var goodtypeid = []
        el.data.data.forEach(item => {
          goodlist.push(item.type_name)
          goodtypeid.push(item.good_type_id)
        });
        goodlist.unshift("全部"),
          goodtypeid.unshift(1),
          that.setData({
            goodlist,
            goodtypeid
          })
      })
      .then(function () {
        that.allgoodshow()
        that.setData({
          loading:false
        })
      })
  },
  //全部商品列表
  Goodlist: function () {
    let that = this
    request({
      url: '/goodList',
    }).then((res) => {
      if (res.data.code == 2000) {
        that.setData({
          countnum: res.data.data.count
        })
        request({
          url: '/goodList',
          data: {
            page: 1,
            size: that.data.countnum
          }
        }).then((res) => {
          that.setData({
            allgood: res.data.data.data
          })
          that.allgoodshow();
        })
      }
    })
  },

  showdataList: function (index) {
    let that = this
    that.setData({
      dataList: that.data.datalist.slice(0, that.data.pagesize * that.data.currPage),
    })

    if(this.data.scrolled){
      if (that.data.pagesize * that.data.currPage >= that.data.list.length) {
        
        if (that.data.datalist.length >= that.data.countnum) {
          
          var timer2=setTimeout(function () {
            that.setData({
              warn: '正在加载中'
            })
          }, 1000)
          var timer3=setTimeout(function () {
            that.setData({
              warn: '没有更多了'
            })
          }, 2000)
          var timer4=setTimeout(function () {
            that.setData({
              warn: ''
            })
          }, 3000)
          that.setData({
            timer2:timer2,
            timer3:timer3,
            timer4:timer4
          })
        } else {
          //请求下一页
          that.setData({
            page: ++that.data.page
          })
          that.showgoodlist(index)
        }
      }
       else {
         
        var timer1=setTimeout(function () {
          that.setData({
            warn:"正在加载中",
            currPage: ++that.data.currPage,
          })
        }, 1000)
        that.setData({
          timer1:timer1
        })
        setTimeout(function(){
          that.setData({
            dataList: that.data.datalist.slice(0, that.data.pagesize * that.data.currPage),
          })
        
        },2000)  
      }
    }
     
  },
  //全部商品渲染接口 
  allgoodshow: function () {
    var that = this
    request({
      url: '/homeGoodList',
      data: {
        token: wx.getStorageSync('token')
      }
    }).then((res) => {
      if (res.data.code == 2000) {
        that.setData({
          All: res.data.data.filter(aa => aa.children.length > 0)
        })
        that.data.All.forEach(el => {
          el.children = el.children.map(item => {
            item["imgUrl"] = item.img
            item["title"] = item.good_name
            return item
          })
        })
        //更改键值后的全部商品列表
        that.setData({
          All: that.data.All
        })
        that.allgoodlist()
      }
    })


  },
  allgoodlist: function () {
    let that = this
    if (that.data.count > that.data.All.length) {
      setTimeout(function () {
        that.setData({
          warn: '没有更多了'
        })
      }, 1000)
      setTimeout(function () {
        that.setData({
          warn: ''
        })
      }, 2000)
    } else {
      that.setData({
        warn: '正在加载中',
        Allshow: that.data.All.slice(0, that.data.count)
      })
    }
    that.setData({
      view: "全部",
      count: ++that.data.count
    })
  },
  viewmore: function (e) {
    this.data.goodtypeid.forEach((item, index) => {
      if (item == e.currentTarget.dataset.id) {
        this.showgoodlist(index);
        this.setData({
          curTab: index,
          current: index
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let _this = this
    _this.setData({
      scrolled:true
    })
    
    if (this.data.view != "其他") {
      _this.allgoodlist()
    } else {
     
      if ((_this.data.pagesize * _this.data.currPage < _this.data.list.length)&&_this.data.list.length>0) {
        
        _this.showdataList()
        // setTimeout(function () {
          
        // }, 1000)
      }
      else{
        var timer2=setTimeout(function () {
          _this.setData({
            warn: '正在加载中'
          })
        }, 1000)
        var timer3=setTimeout(function () {
          _this.setData({
            warn: '没有更多了'
          })
        }, 2000)
        var timer4=setTimeout(function () {
          _this.setData({
            warn: ''
          })
        }, 3000)
        _this.setData({
          timer2:timer2,
          timer3:timer3,
          timer4:timer4
        })
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})